(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.user.deleteController', deleteController);
	deleteController.$inject = [ '$scope', 'Constants', 'modalService', 'layoutConfig', 'validatorConfig', 'gridService', 'authService', 'userService' ];
	function deleteController($scope, Constants, modalService, layoutConfig, validatorConfig, gridService, authService, userService) {
		var ctrl = this, userInfo = $scope.$stateParams.params;
		ctrl.layoutConfig = layoutConfig;
		ctrl.user={};
		ctrl.init = function() {
			ctrl.accessRolePermission = {permission : 'accessRole', authorization : authService};
			// init role
			userService.initRole($scope, ctrl);
			ctrl.roleGridOptions.enableRowHeaderSelection = false;
			// init group
			userService.initGroup($scope, ctrl);
			ctrl.groupGridOptions.enableRowHeaderSelection = false;
			// init token
			userService.initToken($scope, ctrl);
			ctrl.tokenGridOptions.enableRowHeaderSelection = false;
			// init user dynaparm
			userService.initDynaParm($scope, ctrl);
			ctrl.selectedPropertyInnerGridOptions.enableRowHeaderSelection = false;
			if(ctrl.availablePropertyInnerGridOptions){
				ctrl.availablePropertyInnerGridOptions.enableRowHeaderSelection = false;
			}
			userService.loadOptions().then(function(userOptions) {
				userService.initOptions(ctrl, userOptions);
				authService.delete(userInfo).then(function(response) {
					// init user data
					ctrl.user = response.data || userInfo;
					ctrl.user.userTokens = ctrl.user.userTokens || [];
					ctrl.resetDynaProperties();
				});
			});
		};
		
		
		ctrl.deleteSubmit = function() {
			modalService.openConfirmDialogs({
				'messages' : [ {msg : 'title.dialog.confirm.delete', args : [ctrl.user.primaryKey]} ],
				'btnCallback' : {
					'ok' : function(response) {
						authService.deleteSubmit(ctrl.user).then(deleteSuccessful, deleteFailed);
					}
				}
			});
		};
		
		function deleteSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.delete' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.user.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.user.list',
					'state' : 'cda.user.list',
					'params' : {}
				}]
			});
		}

		function deleteFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
	}
})(angular);